package com.nit.bike.controller.basic;

import com.alibaba.fastjson.JSONObject;
import com.nit.bike.dto.basic.BikeDTO;
import com.nit.bike.dto.system.TokenDTO;
import com.nit.bike.dto.utils.OpResultDTO;
import com.nit.bike.dto.utils.TableReqDTO;
import com.nit.bike.dto.utils.TableRspDTO;
import com.nit.bike.service.basic.BikeService;
import com.nit.bike.service.basic.StationService;
import com.nit.bike.utils.AuthorizationUtil;
import com.nit.bike.utils.HttpRequestReader;
import com.nit.bike.utils.JWTUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping(value = "/api/bike")
public class BikeController {

    @Resource
    private BikeService bikeService;
    private final Logger logger = LoggerFactory.getLogger(StationController.class);


    /**
     * 获取数据列表
     */
    @RequestMapping(value = "/query", method = RequestMethod.POST)
    public TableRspDTO list4Table(@RequestHeader(name = "Access-Token") String token, @RequestBody TableReqDTO req) {
        TableRspDTO rsp = new TableRspDTO();
        try {
            TokenDTO tokenDTO = JWTUtil.verifyToken(token);
            if (tokenDTO != null) {
                if (tokenDTO.getRoleId() == 1) {
                    rsp = bikeService.list4Table(req);
                } else {
                    return rsp;
                }
            } else {
                return rsp;
            }
        } catch (Exception e) {
            logger.error(e.toString());
        }
        return rsp;
    }

    /**
     * 获取数据列表
     */
    @RequestMapping(value = "/listByStation", method = RequestMethod.POST)
    public TableRspDTO listByStation(@RequestHeader(name = "Access-Token") String token, @RequestBody TableReqDTO req) {
        TableRspDTO rsp = null;
        try {
            TokenDTO tokenDTO = JWTUtil.verifyToken(token);
            if (AuthorizationUtil.userInterfaceBool(tokenDTO)) {
                rsp = bikeService.listByStation(req);
            }
        } catch (Exception e) {
            logger.error(e.toString());
        }
        return rsp;
    }

    /**
     * 新增记录
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public OpResultDTO add(@RequestHeader(name = "Access-Token") String token, @RequestBody BikeDTO bikeDTO) {
        OpResultDTO op = new OpResultDTO();
        try {
            TokenDTO tokenDTO = JWTUtil.verifyToken(token);
            op.setMessage(bikeService.add(bikeDTO).toString());
        } catch (Exception e) {
            logger.error(e.toString());
            op.setMessage("error");
        }
        return op;
    }

    /**
     * 更新记录
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public OpResultDTO update(@RequestBody BikeDTO bikeDTO) {
        OpResultDTO op = new OpResultDTO();
        try {
            op.setResult(bikeService.update(bikeDTO).toString());
        } catch (Exception e) {
            logger.error(e.toString());
            op.setMessage("error");
        }
        return op;
    }

    /**
     * 删除记录
     */
    @RequestMapping(value = "/remove", method = RequestMethod.POST)
    public OpResultDTO remove(HttpServletRequest request) {
        OpResultDTO op = new OpResultDTO();
        try {
            JSONObject jsonRequest = HttpRequestReader.getJsonObject(request);
            String ids = jsonRequest.getString("ids");
            op.setMessage(bikeService.remove(ids).toString());
        } catch (Exception e) {
            logger.error(e.toString());
            op.setMessage("error");
        }
        return op;
    }

    /**
     * @Description: 更新状态
     */
    @RequestMapping(value = "/updateState", method = RequestMethod.POST)
    public OpResultDTO updateState(HttpServletRequest request) {
        OpResultDTO op = new OpResultDTO();
        try {
            JSONObject jsonRequest = HttpRequestReader.getJsonObject(request);
            String ids = jsonRequest.getString("ids");
            Integer state = jsonRequest.getInteger("state");
            op.setMessage(bikeService.updateState(ids, state).toString());
        } catch (Exception e) {
            logger.error(e.toString());
            op.setMessage("error");
        }
        return op;
    }
}
